package top.callname.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import top.callname.bean.CourseBean;
import top.callname.bean.CourseTimeBean;

public class CourseDao extends BaseDao{

	private Logger logger = Logger.getLogger(CourseDao.class);
	
	
	public List<CourseBean> selectByCid(Integer cid){
		List<CourseBean> list = new ArrayList<CourseBean>();
		String sql = "SELECT  `course_id`,"
				+ "`week_day`, "
				+ "`course_one`,"
				+ "`course_two`, "
				+ "`course_three`, "
				+ "`course_four`, "
				+ "`course_five`, "
				+ "`course_six`, "
				+ "`course_seven`, "
				+ "`course_eight`, "
				+ "`course_nine`, "
				+ "`cid` FROM t_course WHERE cid="+cid +" ORDER BY week_day ASC";
		
		logger.info("根据cid查询课程信息："+sql);
		res = query(sql);
		try {
			while(res.next()){
				CourseBean bean = new CourseBean();
				bean.setCourseId(res.getInt("course_id"));
				bean.setWeekDay(res.getInt("week_day"));
				bean.setCourseOne(res.getString("course_one"));
				bean.setCourseTwo(res.getString("course_two"));
				bean.setCourseThree(res.getString("course_three"));
				bean.setCourseFour(res.getString("course_four"));
				bean.setCourseFive(res.getString("course_five"));
				bean.setCourseSix(res.getString("course_six"));
				bean.setCourseSeven(res.getString("course_seven"));
				bean.setCourseEight(res.getString("course_eight"));
				bean.setCourseNine(res.getString("course_nine"));
				bean.setCid(res.getInt("cid"));
				list.add(bean);
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_course 出错："+e);
		}
		return list;
	}
	
	
	/**
	 * 查询课程时间
	 * @param key
	 * @return CourseTimeBean
	 */
	public CourseTimeBean selectCourseTimeByKey(String key){
		String sql = "SELECT * FROM t_course_time WHERE course_key='"+key+"' ";
		CourseTimeBean bean = new CourseTimeBean();
		logger.info("根据key查询课程时间信息："+sql);
		res = query(sql);
		try {
			if(res.next()){
				bean.setId(res.getInt("id"));
				bean.setCourseKey(res.getString("course_key"));
				bean.setAmPm(res.getString("am_pm"));
				String start = res.getString("start_time");
				bean.setStartTime(start.substring(0,start.length()-3));
				String end = res.getString("end_time");
				bean.setEndTime(end.substring(0,start.length()-3));
			}
			closeDB();
		} catch (SQLException e) {
			logger.error("查询 t_course_time 出错："+e);
		}
		return bean;
	}
	
	
}
